package com.sjx.boundarycallback.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;

import com.sjx.boundarycallback.R;
import com.sjx.boundarycallback.databinding.ActivityWifiBinding;

/**
 * created sjx by 22/5/21
 * WIFI页面
 */
public class WifiActivity extends AppCompatActivity {

    private ActivityWifiBinding mBinding;
    private WifiManager mWifiManager; //WIFI管理器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_wifi);
        mWifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        checkWifiStatus();

        openOrCloseWifi();

    }

    /**
     * open or close wifi
     */
    private void openOrCloseWifi() {
        mBinding.btnOpenWifi.setOnClickListener(v -> {
            boolean flag = mBinding.tvWifiStatus.getText() == getResources().getString(R.string.wifi_is_open);
            if (flag) {
                mBinding.btnOpenWifi.setText(getResources().getString(R.string.open_wifi));
                mBinding.tvWifiStatus.setText(getResources().getString(R.string.wifi_is_close));
                mWifiManager.setWifiEnabled(false);
            } else {
                mBinding.btnOpenWifi.setText(getResources().getString(R.string.close_wifi));
                mBinding.tvWifiStatus.setText(getResources().getString(R.string.wifi_is_open));
                mWifiManager.setWifiEnabled(true);
            }
        });

    }

    /**
     * Check if wifi is on or close
     */
    private void checkWifiStatus() {
        if (mWifiManager.isWifiEnabled()) {
            mBinding.tvWifiStatus.setText(getResources().getString(R.string.wifi_is_open));
            mBinding.btnOpenWifi.setText(getResources().getString(R.string.close_wifi));
        } else {
            mBinding.tvWifiStatus.setText(getResources().getString(R.string.wifi_is_close));
            mBinding.btnOpenWifi.setText(getResources().getString(R.string.open_wifi));
        }
    }
}